import {
    ElButton,
    ElIcon,
    ElTag,
    ElTooltip,
    TableV2FixedDir,
    ElMessageBox,
    ElMessage,
    ElSelect,
    ElOption,
    ElLink
  } from 'element-plus'
  import { getFile } from "@/api";
import {dayfrom} from "@/utils/dayfrom.js"
//   @element-plus/icons-vue
  import { h } from 'vue'

  let tagTypes = [['success','进行中（待办）'],['primary','已完成（已办）'],['danger','已逾期（未完成）']]
  // let tagTypes = new Map(
  //   [
  //     ['已完成', 'primary'],
  //     ['进行中', 'success'],
  //     ['进行中（已到期）', 'warning'],
  //     ['进行中（已逾期）', 'danger']
  //   ]
  // )
const userAllTaskColumn = {
    "id": "",//任务id    
    "userId": "",//任务所属用户id
    "appointorId":"" ,//指派人id（自己创建：NULL，被派发：绑定管理员id）
    "workStatusId":"" ,//任务状态id（0：进行中（待办），1:已完成（已办），2:已逾期（未完成））
    "priorityId": "",//任务的优先级(0:一般，1:重要，2:紧急)
    "attachmentsId":"" ,//附件id（没有附件默认NULL）
    "title": "",//标题
    "content": "",//任务的详细描述
    "require": "",//任务要求
    "createTime": "",//创建任务的时间戳
    "updateTime": "",//最后一次更新的时间戳
    "endTime": "",//任务结束的时间戳
    "deletionState": ""//逻辑删除（0未删除，1逻辑删除），用来定期清理删除已删除任务的附件
}


export const getColumns =  function(router,store){
  return [
  
    {
        align: 'center',
        width: 150,
        key: "title", 
        dataKey: "title", 
        title: '任务名称'
      },
      {
        align: 'center',
        width: 150,
        key: "content", 
        dataKey: "content", 
        title: '任务内容'
      },
      {
        align: 'center',
        width: 150,
        key: "attachmentName", 
        dataKey: "attachmentName", 
        title: '附件',
        cellRenderer : ({ rowData }) => {
          const onClick = () => {
            getFile(rowData.attachmentName).then(res => {
              let contentType = res.headers['content-type'] || 'application/octet-stream';
              var blob = new Blob([res.data], { type: contentType });
              var a = document.createElement("a");
              const urlObject = URL.createObjectURL(blob);
              a.href = urlObject
              a.download = rowData.attachmentName;
  
              a.click();
              URL.revokeObjectURL(urlObject);
  
            })
          }
            // return rowData.attachmentsId==null?"/":rowData.attachmentsId
             return rowData.attachmentName == null || rowData.attachmentName == "" ? "/" : h(ElLink, { onClick, underline:false }, () => rowData.attachmentName)
        }
      },
      {
        align: 'center',
        width: 150,
        key: "userName", 
        dataKey: "userName", 
        title: '姓名',
        cellRenderer:({ rowData }) => {
          return (rowData.userName==null||rowData.userName=="")?"/":((rowData.appointorName==null||rowData.appointorName=="")?rowData.userName:rowData.appointorName)
        }
        
      },
      {
        align: 'center',
        width: 150,
        key: "appointorId", 
        dataKey: "appointorId", 
        title: '任务类型',
        cellRenderer : ({ rowData }) => {
            return rowData.appointorId==null?"自主创建":"被指派"
          }
      },
      {
        align: 'center',
        width: 150,
        key: "workStatusName", 
        dataKey: "workStatusName", 
        title: '任务状态',
        cellRenderer : ({ rowData }) => {
        
          let color = "black;"
            if(rowData.workStatusId==2){
                color = "red;"
            }
            return h('div', null, [
              h(ElTag, { type: tagTypes[rowData.workStatusId][0], effect: "dark", round: true, class: "tagStatus"}),
              h('span', { style:`color:${color}`}, rowData.workStatusName||tagTypes[rowData.workStatusId][1])
            ]);
          }
      },
      {
        align: 'center',
        width: 150,
        key: "endTime", 
        dataKey: "endTime", 
        title: '截止时间',
        cellRenderer : ({ rowData }) => {
            return  dayfrom(rowData.endTime*1)
          }
      },
      {
        align: 'center',
        width: 150,
        key: "createTime", 
        dataKey: "createTime", 
        title: '创建时间',
        cellRenderer : ({ rowData }) => {
            return  dayfrom(rowData.createTime*1)
          }
      },
      {
        align: 'center',
        width: 150,
        key: "updateTime", 
        dataKey: "updateTime", 
        title: '完成时间',
        cellRenderer : ({ rowData }) => {
            return rowData.workStatusId==1?dayfrom(rowData.updateTime*1):"/"
          }
      },
      {
        key: 'operations',
        title: '操作',
        fixed: TableV2FixedDir.RIGHT,
        cellRenderer: ({ rowData }) => {
          const onEditClick = () => {
             router.push({ path: "usereditTaskPages", query: { tasks: [JSON.stringify(rowData)] } })
          }
      
          const onDeleteClick = () => {
            ElMessageBox.confirm('确定删除吗?')
              .then(() => {
                store.deleteTasks({taskIds:[rowData.id]})//调用删除任务

              })
              .catch((err) => {  
                console.log(err);
                
                ElMessage({
                  message: '已取消',
                  type: 'warning',
                })
              })
          }
          if(rowData.appointorId==null){

          }
          let isDisabled= rowData.appointorId==null?false:true
          return h('div', null, [
            h(ElButton, { size: "small", onClick: onEditClick }, () => '编辑'), 
            h(ElButton, { size: "small", type: "danger", onClick: onDeleteClick,disabled:isDisabled }, () => '删除')
          ])
        },
        width: 150,
        align: 'center',
      
      }
]
}




